Repository navigation
Fix operands of arithmetic operators being type checked twice - #9501
Conversation
Why:
Typecore.translate_unified_ops types each operand of an operator
such as `+` without an expected type, to choose the operator's type
from it. It then typed an operand again against the chosen type. An
operand that is itself an operator application did the same inside,
so the work doubled with each level of nesting, and diagnostics on
an operand, such as a @ppwarning, were reported again each time.
An operand is now given the chosen type by unifying its type with a
fresh instance of it. Only when that fails is it typed again against the
chosen type, for the same error message as before.
Diagnostic modified (@ppwarning on a nested operand):
Example:
let x = 1 + (1 + (1 + (@ppwarning("operand") 2)))
Before (diagnostic):
Warning number 22 (reported 8 times)
operand
After (diagnostic):
Warning number 22 (reported once)
operand
Fixes #9500
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gH9cXWp3xtBmfRV3Du
Signed-off-by: Christoph Knittel <ck@cca.io>
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Changelog🐛 Bug fix
Semantic Diff3 commits · Full page Fix operands of arithmetic operators being type checked twice2149039 · cknitt + Claude · Dec 01, 2026, 20:26 UTC
An operand is now given the chosen type by unifying its type with a fresh instance of it. Only when that fails is it typed again against the chosen type, for the same error message as before. Diagnostic modified ( let x = 1 + (1 + (1 + (@ppwarning("operand") 2)))
-Warning number 22 (reported 8 times)
+Warning number 22 (reported once)
operandFixes #9500 Test the typing rules of arithmetic operators on variable operands65b6611 · cknitt + Claude · Dec 01, 2026, 20:35 UTC The fix keeps an operand's first typed tree when its type fits, so each way an operator takes its type needs a test that compiles and runs: from the left operand, from the right operand, the fallback to int, and nested operators. Each function only compiles if its operands get the right type. ✨ Test operands that are typed again and left operands typed twice before2b8872c · cknitt + Claude · Dec 01, 2026, 20:58 UTC · new A review found two gaps in the tests. Typing an operand again is also how an operand whose type is int only inside a GADT branch type checks, and no test covered that. And the left operand in ppwarning_operand_once was an int, which was never typed twice, so it did not test the fix. It is replaced by left operands that take their type from the right operand or fall back to int. The changelog no longer says the cost grew by one typing per level: it doubled. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 21490390d0
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
🎉 Compiler benchmarksAllocated wordsIn total: 655,165,618 → 650,988,316 (-0.64%). Generated projects: 627,287,779 → 626,962,875 (-0.05%). 🎉 Improvement: 4 benchmarks allocate more than 1% less.
16 more changes
InstructionsIn total: 9,128,411,164 → 8,961,649,825 (-1.83%). 🎉 Improvement: 3 benchmarks execute more than 1% fewer instructions.
Output sizeIn total: 27,368,788 → 27,371,412 (+0.01%). None of the 13 benchmarks changed by 0.1% or more. Pull request 2b8872c compared with base 69e11bf in this run. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #9501 +/- ##
==========================================
- Coverage 82.49% 82.49% -0.01%
==========================================
Files 467 467
Lines 66125 66120 -5
==========================================
- Hits 54553 54548 -5
Misses 11572 11572
🚀 New features to boost your workflow:
|
The fix keeps an operand's first typed tree when its type fits, so each way an operator takes its type needs a test that compiles and runs: from the left operand, from the right operand, the fallback to int, and nested operators. Each function only compiles if its operands get the right type. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kbf3gH9cXWp3xtBmfRV3Du Signed-off-by: Christoph Knittel <ck@cca.io>
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
Developer playground preview: https://rescript-lang.github.io/rescript/dev-playground/?version=pr-9501 |
A review found two gaps in the tests. Typing an operand again is also how an operand whose type is int only inside a GADT branch type checks, and no test covered that. And the left operand in ppwarning_operand_once was an int, which was never typed twice, so it did not test the fix. It is replaced by left operands that take their type from the right operand or fall back to int. The changelog no longer says the cost grew by one typing per level: it doubled. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kbf3gH9cXWp3xtBmfRV3Du Signed-off-by: Christoph Knittel <ck@cca.io>
Fixes #9500.
The operands of an arithmetic operator such as
+were type checked twice, and each nested operator doubled that again:Typecore.translate_unified_opstypes each operand without an expected type, to choose the operator's type from them. It then typed an operand again against the chosen type. An operand that is itself an operator application did the same inside, so the work doubled at each level. Besides repeating diagnostics, this made a right-nested chain slow: 18 levels of1 + (…)took 1.5 s to compile, now 0.04 s.The fix. An operand gets the chosen type by unifying its type with a fresh instance of it. Only when that fails is it typed again against the chosen type, as before. That covers wrong operand types, which keep their old error message, and operands whose type equals the chosen type only through a GADT equation, which still type check. The instance matters: unifying with the shared predefined
intitself would link that node to the operand's type, and every later use ofintwould see that type.Tests.
ppwarning_operand_once: a@ppwarningon a nested int operand, a float operand, a left operand that takes its type from the right one, and both operands of an int fallback, each reported once.unified_ops_operand_test: variable operands that take their type from the left operand, from the right operand, or fall back to int, an operand whose type is int only inside a GADT branch, and nested operators. Each function only compiles if its operands get the right type, and the tests check the results.Not changed. When the first attempt fails and the operand is typed again, its diagnostics are still reported twice, as before, for example for an operand of the wrong type.
🤖 Generated with Claude Code
https://claude.ai/code/session_01Kbf3gH9cXWp3xtBmfRV3Du